package cn.nku.shichangwei;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TaDao extends BaseDao{
	public boolean addTa(Ta ta){
		String sql="insert into ta values(?,?)";
		try {
			java.sql.PreparedStatement pre=con.prepareStatement(sql);
			pre.setString(1,ta.getTaid());
			pre.setString(2,ta.getCouid());
			if(pre.executeUpdate()>0)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}

	public List<Ta> selectTa(Ta t) {
		// TODO Auto-generated method stub
		String sql="select * from ta ";
		List<Ta> retList=new ArrayList<>();
		if(t.getTaid()!=null&&t.getTaid().length()!=0){
			sql+="where stuid="+t.getTaid();
		}
		java.sql.PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			ResultSet executeQuery=pre.executeQuery();
			while(executeQuery.next()){
				Ta ta=new Ta();
				ta.setTaid(executeQuery.getString("stuid"));
				ta.setCouid(executeQuery.getString("couid"));
				retList.add(ta);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retList;
	}

	public boolean update(Ta t) {
		// TODO Auto-generated method stub
		String sql = "update ta set couid=? where stuid=?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, t.getCouid());
			preparedStatement.setString(2, t.getTaid());
			if(preparedStatement.executeUpdate() > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public boolean delete(String id) {
		// TODO Auto-generated method stub
		String sql="delete from ta where stuid=?";
		java.sql.PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, id);
			if(pre.executeUpdate()>0)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
